import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


public class TCPFileServer extends FileServer {

	private ServerSocket server;
	
	public TCPFileServer(int port) throws IOException {
		super(port);
		server = new ServerSocket(port);
	}

	@Override
	public void serve() {
		try
		{
			while (true)
			{
				try {
					Socket client = server.accept();
					BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream()));
					PrintWriter w = new PrintWriter(client.getOutputStream(), true);
					w.println("Welcome to the Java TCP File Server.  Type 'bye' to close.");
					String line;
					String message;
					boolean isPost = false;
					
					FileInputStream inf = null;
					String fileName = "";
					File f;
					int bufferSize = 8192;
					byte[] buffer = new byte[bufferSize];
					int count = -1;
					int part = 0;
					do
					{
						line = r.readLine();
						message = "NULL";
						if(line.equalsIgnoreCase("list")){
							ArrayList<String> list = getFileList();
							if(list == null || list.size() == 0)
								message = "201";
							else{
								message = "200";
								for(String i:list){
									message += " " + i + ";";
								}
							}
							w.println(message);
							continue;
						}
						
						if(line.length() >= 4 && line.substring(0, 4).equalsIgnoreCase("get ")){
							fileName = line.substring(4).trim();
							f = getAFileContent(fileName);
							
							if(f == null){
								message = "101";
							}
							else{
								message = "100";
								isPost = true;
								part = 0;
								inf = new FileInputStream(f);
							}
							w.println(message);
							continue;
						}
						
						if(isPost && line.equalsIgnoreCase("get")){
							count = -1;
							if(inf!=null){
								count = inf.read(buffer, 0, bufferSize);
							}
							if(count <= -1){
								isPost = false;
								w.println("199");
								inf.close();
								continue;
							}
							
							String base64 = FileUtility.BytesToBase64(buffer, count);
							
							w.println("<file>" + fileName + "</file><part>" + part + "</part><length>" + count + "</length><data>" + base64 + "</data>");
							part ++;
							Thread.sleep(100);
							continue;
						}
						w.println(message);
					}
					while ( !line.trim().equals("bye") );
					client.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		catch (Exception err)
		{
			System.err.println(err);
		}
		
	}

	public void send(byte[] content) {
		// TODO Auto-generated method stub
		
	}

	public byte[] receive() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void showInfo() {
		System.out.println("TCP File Server");
		System.out.println("Port: " + getPort());
	}

}
